﻿using System;
using CoreTypes.Properties;

namespace CoreTypes
{
   public class ServiceOperationException : Exception
   {
      string FErrorCode;
      ErrorReasonCode FReasonCode = ErrorReasonCode.Unknown;

      public string Reason
      {
         get
         {
            switch (FReasonCode)
            {
               case ErrorReasonCode.Unknown:
               {
                  return "";
               }
               case ErrorReasonCode.Timeout:
               {
                  return Resources.S_The_operation_was_timed_out;
               }
               case ErrorReasonCode.Network:
               {
                  return Resources.S_Network_problems;
               }
               case ErrorReasonCode.Credentials:
               {
                  return Resources.S_Invalid_login_password;
               }
               case ErrorReasonCode.ExistAlready:
               {
                  return Resources.S_Wait_while_previos_operation_complete;
               }
               case ErrorReasonCode.NotSupported:
               {
                  return Resources.S_This_operation_is_not_supported;
               }
               case ErrorReasonCode.DeliverFailed:
               {
                  return Resources.S_Message_deliver_failed;
               }
               case ErrorReasonCode.ServerInternal:
               {
                  return Resources.S_Internal_server_error_occured;
               }
               case ErrorReasonCode.ServiceFailure:
               {
                  return Resources.S_Connection_to_the_service_is_faulted;
               }
               case ErrorReasonCode.EmptyArguments:
               {
                  return Resources.S_This_operation_requires_non_empty_argument_list;
               }
               case ErrorReasonCode.LoggedInAlready:
               {
                  return Resources.S_You_are_already_logged_in;
               }
               case ErrorReasonCode.InvalidArguments:
               {
                  return Resources.S_Invalid_arguments_provided;
               }
               case ErrorReasonCode.NoMeaningfulResult:
               {
                  return Resources.S_Server_did_not_provided_any_meaningful_result;
               }
               case ErrorReasonCode.ConnectionRequired:
               {
                  return Resources.S_Connection_to_the_server_required;
               }
               default:
               {
                  return FReasonCode.ToString();
               }
            }
         }
      }
      public string ErrorCode
      {
         get
         {
            return FErrorCode;
         }
      }
      public override string Message
      {
         get
         {
            string message = base.Message, reason = Reason;

            if (message != "" && reason != "") message += "\r\n\r\n";
            if (reason != "") message += reason;

            return message;
         }
      }
      public ErrorReasonCode ReasonCode
      {
         get
         {
            return FReasonCode;
         }
      }

      public ServiceOperationException(string message)
         : base(message)
      {
      }

      public ServiceOperationException(ErrorReasonCode reason)
         : base("")
      {
         FReasonCode = reason;
      }

      public ServiceOperationException(string message, Exception innerException)
         : base(message, innerException)
      {
      }

      public ServiceOperationException(ErrorReasonCode reason, Exception innerException)
         : base("", innerException)
      {
         FReasonCode = reason;
      }

      public ServiceOperationException(string errorCode, string message)
         : base(message)
      {
         FErrorCode = errorCode;
      }

      public ServiceOperationException(ErrorReasonCode reason, string message)
         : base(message)
      {
         FReasonCode = reason;
      }

      public ServiceOperationException(ErrorReasonCode reason, string message, Exception innerException)
         : base(message, innerException)
      {
         FReasonCode = reason;
      }
   }
}